package com.dopplr.examples.modestmaps
{
    import com.dopplr.api.API;
    import com.dopplr.api.Traveller;
    import com.modestmaps.Map;
    import com.modestmaps.extras.MapControls;
    import com.modestmaps.geo.Location;
    import com.modestmaps.mapproviders.microsoft.MicrosoftRoadMapProvider;
    
    import flash.display.Sprite;
    import flash.display.LoaderInfo;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.ErrorEvent;
    import flash.events.Event;
    import flash.events.StatusEvent;

    [SWF(backgroundColor="#ffffff")]
    public class DopplrCityMap extends Sprite
    {
        public var api:API;
        public var map:Map;
        
        public var icons:Array = [];
        
        public function DopplrCityMap()
        {
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            
            var token:String = LoaderInfo(root.loaderInfo).parameters.token;
            if (token) {        
                map = new Map(stage.stageWidth, stage.stageHeight, true, new MicrosoftRoadMapProvider(true));
                map.setCenterZoom(new Location(0,0), 2);
                addChild(map);
                map.addChild(new MapControls(map));
                
                stage.addEventListener(Event.RESIZE, function(event:Event):void { map.setSize(stage.stageWidth, stage.stageHeight); });
                
                api = new API(token);
            
                api.addEventListener(ErrorEvent.ERROR, onAPIError);
                api.addEventListener(StatusEvent.STATUS, onAPIStatus);
    
                // try loading the user's fellow travellers
                api.loadFellowTravellers(gotEm);
            }
            else {
                trace("problem initializing dopplr access: no token found");
            }
        }
        
        public function gotEm(users:Array):void
        {
            // fellow travellers don't come with home or current cities, so...
            for each (var user:Traveller in users) {
                api.loadTravellerInfo(user.nick, gotHome);
            }
            
            // could also get them all at once, but it's a long call
            // api.loadTravellerInfo(users, gotHomes);
        }
        
        public function gotHome(user:Traveller):void
        {
            // get the marker for the user's current city
            var marker:DopplrCityMarker = map.getMarker(user.currentCity.geonameId.toString()) as DopplrCityMarker;
            if (!marker) {
                // create a marker if it didn't exist already
                marker = new DopplrCityMarker(user.currentCity);
                map.putMarker(marker.name, marker.location, marker);
            }
            // add the user to the marker
            marker.addUser(user);
            var rows:int = Math.ceil(Math.sqrt(marker.users.length));
            for (var i:int = 0; i < marker.users.length; i++) {
                var icon:DopplrIcon = marker.users[i];
                icon.x = -(icon.width*rows/2) + icon.width * (i%rows);
                icon.y = -(icon.width*rows/2) + icon.height * Math.floor(i/rows);
                //icon.scaleX = icon.scaleY = 1/marker.users.length;
            }
            //marker.scaleX = marker.scaleY = 1.0/rows;
        }        
        
        public function onAPIError(event:ErrorEvent):void
        {
            // do something about errors here
            var errorText:String = event.text;
            trace(errorText);
        }

        public function onAPIStatus(event:StatusEvent):void
        {
            trace(event.level + ": " + event.code);
        }
                
    }
}

import flash.display.Sprite;
import com.dopplr.api.Traveller;
import com.modestmaps.geo.Location;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.LoaderInfo;
import com.dopplr.api.City;   

class DopplrCityMarker extends Sprite
{
    public var city:City;
    public var location:Location;

    // DopplrIcons for users in this city
    public var users:Array = [];
    
    public function DopplrCityMarker(city:City)
    {
        this.city = city;
        this.location = new Location(city.latitude, city.longitude);
        this.name = city.geonameId.toString();
    }
        
    public function addUser(user:Traveller):DopplrIcon
    {
        var icon:DopplrIcon = new DopplrIcon(user);
        addChild(icon);
        users.push(icon);
        return icon;
    }
}

class DopplrIcon extends Sprite
{
    public var user:Traveller;
        
    public function DopplrIcon(user:Traveller)
    {
        this.user = user;
        this.name = user.nick;
        this.cacheAsBitmap = true;
        
        var loader:Loader = new Loader();
        loader.load(new URLRequest(user.iconURL));
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onIconLoaded);
        addChild(loader);

        graphics.beginFill(user.currentCity.rgb);
        graphics.drawRect(-19, -19, 38, 38);
    }
    
    private function onIconLoaded(event:Event):void
    {
        var loader:Loader = (event.target as LoaderInfo).loader;
        loader.x = -loader.width/2;
        loader.y = -loader.height/2;        
    }
}

